package com.unittec.zk.provider.exam.controller;

import com.alibaba.fastjson.JSON;
import com.unittec.zk.provider.exam.service.ElExamService;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.utils.ValidationUtils;
import com.unittec.zk.sdk.exam.request.*;
import com.unittec.zk.sdk.exam.response.Res201000;
import com.unittec.zk.sdk.log.enums.LogOperateEnum;
import com.unittec.zk.sdk.log.enums.LogStatusEnum;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.BizCode;
import com.unittec.zk.sdk.root.enums.BizCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags = "考试管理")
public class ExamController {

    @Resource
    private Log log;

    @Resource
    private ElExamService elExamService;

    //考试-分页
    @RequestMapping(value = "/201012", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "考试-分页", notes = "考试-分页")
    public Result<Res201000> getExamPage(@RequestBody GatewayRequest<Req201012> req) {
        log.debug(req, "getExamPage {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elExamService.getExamPage(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201012", JSON.toJSONString(req), "考试-分页", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getExamPage {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201012", JSON.toJSONString(req), "考试-分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201012", JSON.toJSONString(req), "考试-分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //考试-增/改
    @RequestMapping(value = "/201013", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "考试-增/改", notes = "考试-增/改")
    public Result<Res201000> saveOrUpdateExam(@RequestBody GatewayRequest<Req201013> req) {
        log.debug(req, "saveOrUpdateExam {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elExamService.saveOrUpdateExam(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201013", JSON.toJSONString(req), "考试-增/改", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "saveOrUpdateExam {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201013", JSON.toJSONString(req), "考试-增/改", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201013", JSON.toJSONString(req), "考试-增/改", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //考试-详情分页查
    @RequestMapping(value = "/201014", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "考试-详情分页查", notes = "考试-详情分页查")
    public Result<Res201000> getExamDetailPage(@RequestBody GatewayRequest<Req201014> req) {
        log.debug(req, "getExamDetailPage {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elExamService.getExamDetailPage(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201014", JSON.toJSONString(req), "考试-详情分页查", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getExamDetailPage {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201014", JSON.toJSONString(req), "考试-详情分页查", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201014", JSON.toJSONString(req), "考试-详情分页查", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //考试-批量删
    @RequestMapping(value = "/201015", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "考试-批量删", notes = "考试-批量删")
    public Result<Res201000> delExamByIds(@RequestBody GatewayRequest<Req201015> req) {
        log.debug(req, "delExamByIds {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elExamService.delExamByIds(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201015", JSON.toJSONString(req), "考试-批量删", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "delExamByIds {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201015", JSON.toJSONString(req), "考试-批量删", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201015", JSON.toJSONString(req), "考试-批量删", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //考试明细
    @RequestMapping(value = "/201016", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "考试明细", notes = "考试明细")
    public Result<Res201000> getPageDetail(@RequestBody GatewayRequest<Req201016> req) {
        log.debug(req, "getPageDetail {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elExamService.getPageDetail(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201016", JSON.toJSONString(req), "考试明细", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getPageDetail {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201016", JSON.toJSONString(req), "考试明细", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201016", JSON.toJSONString(req), "考试明细", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

}
